#include "test_list.h"

#ifdef TEST_SCANDIR

#define TEST_CASE_NAME os
#define TEST_NUMBER(n) Scandir##_##n


#include <os/scandir.h>
#include <os/xcommand.h>

using namespace os;

#define TEST_SCANDIR_000
#define TEST_SCANDIR_001
#define TEST_SCANDIR_002
#define TEST_SCANDIR_003
#define TEST_SCANDIR_004
#define TEST_SCANDIR_005
#define TEST_SCANDIR_006
#define TEST_SCANDIR_007
#define TEST_SCANDIR_008
#define TEST_SCANDIR_009
#define TEST_SCANDIR_010
#define TEST_SCANDIR_011
#define TEST_SCANDIR_012
#define TEST_SCANDIR_013
#define TEST_SCANDIR_014
#define TEST_SCANDIR_015
#define TEST_SCANDIR_016
#define TEST_SCANDIR_017
#define TEST_SCANDIR_018
#define TEST_SCANDIR_019
#define TEST_SCANDIR_020
#define TEST_SCANDIR_021
#define TEST_SCANDIR_022
#define TEST_SCANDIR_023
#define TEST_SCANDIR_024
#define TEST_SCANDIR_025
#define TEST_SCANDIR_026
#define TEST_SCANDIR_027
#define TEST_SCANDIR_028
#define TEST_SCANDIR_029
#define TEST_SCANDIR_030
#define TEST_SCANDIR_031
#define TEST_SCANDIR_032

//------------------------------------------------------------------------------------------------------//
namespace{

    const os::path& start = WhereProgramIsRunning();
    const os::path& work = start/"tmp";

    void CreateTestWorkspace()
    {
        XCreateFile(work/"external/full/_additional/add.h");
        XCreateFile(work/"external/full/.additional/add.h");
        XCreateFile(work/"external/full/additional/add.h");
        XCreateFile(work/"external/full/full.h");
        XCreateFile(work/"external/full/file-32.lib");
        XCreateFile(work/"external/full/file-32-d.lib");
        XCreateFile(work/"external/full/.file-64.lib");
        XCreateFile(work/"external/full/_file-64.lib");
        XCreateFile(work/"external/full/file-64.lib");
        XCreateFile(work/"external/full/file-64-d.lib");
    }


    SetPath* gFiles;
    SetPath* gDirs;
    path*    gPath;

    bool ProccessedFiles(SetPath& files)
    {
        typedef SetPath::const_iterator
            It;

        for(It i = files.begin(), e = files.end(); i!=e; ++i)
            gFiles->insert(*i);
        return false;
    }
    bool ProccessedDirs(SetPath& dirs)
    {
        typedef SetPath::const_iterator
            It;
        for(It i = dirs.begin(),e = dirs.end(); i!=e; ++i)
            gDirs->insert(*i);
        return false;
    }
    void ExitFromDirectory(const path& p){ *gPath = p; }
}
//------------------------------------------------------------------------------------------------------//

#ifdef TEST_SCANDIR_000
TEST(TEST_CASE_NAME, TEST_NUMBER(000))
{
    if(Exists(work))
        EXPECT_EQ(true,RemoveAll(work));

    CreateTestWorkspace();

    const path location = work/"external/full";
    SetMask efiles;  
    efiles.insert("_*");
    efiles.insert(".*");
    SetMask edirs;   
    edirs.insert("_*");
    edirs.insert(".*");

    SetMask ifiles;  
    ifiles.insert("*-64*.lib");

    SetMask idirs;

    SetPath dirs;
    SetPath files;

    ScanDirectory(location,efiles,edirs,ifiles,idirs,dirs,files);

    EXPECT_EQ(2,files.size());
    EXPECT_EQ(1,dirs.size());

    if(Exists(work))
        EXPECT_EQ(true,RemoveAll(work));
}
#endif // TEST_SCANDIR_000


#ifdef TEST_SCANDIR_001
TEST(TEST_CASE_NAME,TEST_NUMBER(001))
{
    if(Exists(work))
        EXPECT_EQ(true,RemoveAll(work));

    CreateTestWorkspace();

    SetPath resultFiles;
    SetPath resultDirs;
    path    resultPath;

    gFiles = &resultFiles;
    gDirs  = &resultDirs;
    gPath  = &resultPath;

    Scanner()
        .ExcludeFileMask(".*")
        .ExcludeFileMask("_*")
        .ExcludeDirMask(".*")
        .ExcludeDirMask("_*")
        .ProcessedFiles( &ProccessedFiles )
        .ProcessedDirectories( &ProccessedDirs )
        .ProcessedExitFromDirectory( &ExitFromDirectory )
    .Search(work/"external/full");

    EXPECT_EQ(6, resultFiles.size() );
    EXPECT_EQ(1, resultDirs.size()  );

    const str name = resultPath.filename().string();
    
    EXPECT_EQ(true, name == "full" );

    if(Exists(work))
        EXPECT_EQ(true,RemoveAll(work));
}
#endif // TEST_SCANDIR_001

#ifdef TEST_SCANDIR_002
TEST(TEST_CASE_NAME,TEST_NUMBER(002))
{
    typedef SetPath::const_iterator
        It;

    if(Exists(work))
        EXPECT_EQ(true,RemoveAll(work));

    CreateTestWorkspace();

    struct TestScanner: Scanner
    {
       ~TestScanner(){}
        TestScanner()
            :mFiles()
            ,mDirs()
            ,mPath()
        {
            ExcludeFileMask(".*");
            ExcludeFileMask("_*");
            ExcludeDirMask(".*");
            ExcludeDirMask("_*");
            Search(work/"external/full");

            EXPECT_EQ(5, mFiles.size());
            EXPECT_EQ(0, mDirs.size() );

            const str name = mPath.filename().string();
            EXPECT_EQ(true, name == "full");
        }

        virtual bool ProccessedFiles(SetPath& f)
        { 
            for(It i = f.begin(),e = f.end(); i!=e; ++i)
                mFiles.insert(*i);
            return true;
        }
        virtual bool ProccessedDirs(SetPath& d)
        {
            for(It i = d.begin(),e = d.end(); i!=e; ++i)
                mDirs.insert(*i);
            return true;
        }
        virtual void LeaveDirectory(const path& p)
            { mPath = p; }

        SetPath mFiles;
        SetPath mDirs;
        path    mPath;
    } scan;

    if(Exists(work))
        EXPECT_EQ(true,RemoveAll(work));
}
#endif // TEST_SCANDIR_002

#ifdef TEST_SCANDIR_003
#endif // TEST_SCANDIR_003

#ifdef TEST_SCANDIR_004
#endif // TEST_SCANDIR_004

#ifdef TEST_SCANDIR_005
#endif // TEST_SCANDIR_005

#ifdef TEST_SCANDIR_006
#endif // TEST_SCANDIR_006

#ifdef TEST_SCANDIR_007
#endif // TEST_SCANDIR_007

#ifdef TEST_SCANDIR_008
#endif // TEST_SCANDIR_008

#ifdef TEST_SCANDIR_009
#endif // TEST_SCANDIR_009

#ifdef TEST_SCANDIR_010
#endif // TEST_SCANDIR_010

#ifdef TEST_SCANDIR_011
#endif // TEST_SCANDIR_011

#ifdef TEST_SCANDIR_012
#endif // TEST_SCANDIR_012

#ifdef TEST_SCANDIR_013
#endif // TEST_SCANDIR_013

#ifdef TEST_SCANDIR_014
#endif // TEST_SCANDIR_014

#ifdef TEST_SCANDIR_015
#endif // TEST_SCANDIR_015

#ifdef TEST_SCANDIR_016
#endif // TEST_SCANDIR_016

#ifdef TEST_SCANDIR_017
#endif // TEST_SCANDIR_017

#ifdef TEST_SCANDIR_018
#endif // TEST_SCANDIR_018

#ifdef TEST_SCANDIR_019
#endif // TEST_SCANDIR_019

#ifdef TEST_SCANDIR_020
#endif // TEST_SCANDIR_020

#ifdef TEST_SCANDIR_021
#endif // TEST_SCANDIR_021

#ifdef TEST_SCANDIR_022
#endif // TEST_SCANDIR_022

#ifdef TEST_SCANDIR_023
#endif // TEST_SCANDIR_023

#ifdef TEST_SCANDIR_024
#endif // TEST_SCANDIR_024

#ifdef TEST_SCANDIR_025
#endif // TEST_SCANDIR_025

#ifdef TEST_SCANDIR_026
#endif // TEST_SCANDIR_026

#ifdef TEST_SCANDIR_027
#endif // TEST_SCANDIR_027

#ifdef TEST_SCANDIR_028
#endif // TEST_SCANDIR_029

#ifdef TEST_SCANDIR_030
#endif // TEST_SCANDIR_030

#ifdef TEST_SCANDIR_031
#endif // TEST_SCANDIR_031

#ifdef TEST_SCANDIR_032
#endif // TEST_SCANDIR_032

#endif



